<template>
  <div class="login">
    <el-form class="login-form" ref="ruleFormRef" :model="ruleForm"  status-icon
    :rules="rules">
      <div class="title">登录页</div>
      <el-form-item prop="phone">
        <el-input
          placeholder="请输入账号"
          v-model="ruleForm.phone"
          size="large"
          type="text"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item prop="pass">
        <el-input
          placeholder="请输入密码"
          size="large"
          v-model="ruleForm.pass"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref ,Ref} from "vue";
import service from "../utlis/service";
import { useRouter } from "vue-router";
import { ElNotification } from "element-plus";
import "element-plus/es/components/notification/style/css"



const $router = useRouter();

const ruleForm = reactive({
  phone: "",
  pass: "",
});
 

const validatePhone = (rule:any, value:any, callback:any) => {
  if (value === "") {
    callback(new Error("用户名不能为空"));
  }else if( /^[\w-]{4,16}$/.test( value ) == false ){
    callback(new Error('请输入正确的账号'))

  }else{
    callback()
  }

  
  
}
const validatePass = (rule:any, value:any, callback:any) => {

  if (value === "") {
    callback(new Error("密码不能为空"));
  }else if(/^\d{6}$/.test( value ) == false ){
    callback(new Error('请输入正确的密码'))

  }else{
    callback()
  }

  // if () {
  //   callback(new Error('请输入符合要求的密码,'))
  // } else {
  //   callback()
  // }
}
//表单验证规则
const rules = reactive({
  phone: [{ validator: validatePhone, trigger: 'blur' }],
  pass: [{ validator: validatePass, trigger: 'blur' }]
})


const login = () => {
  service({
    url: "/admin/login",
    method: "post",
    data: {
      adminname: ruleForm.phone,
      password: ruleForm.pass,
    },
  }).then((res) => {
    switch (res.data.code) {
      case "200": {
       
        //保存userdname
        sessionStorage.setItem("username", res.data.data.adminname);
        //保存token
        sessionStorage.setItem("token", res.data.data.token);

        // 存储当前用户的权限
        sessionStorage.setItem(
          "grants",
          JSON.stringify(res.data.data.checkedkeys)
        );
        $router.push("/Adman");
        ElNotification({
          title: "登录成功",
          type: "success",
        });
        break;
      }
      case "10003": {
        ElNotification({
          title: "密码错误!",
          type: "success",
        });

        break;
      }
      case "10005": {
        ElNotification({
          title: "未注册",
          type: "success",
        });
        break;
      }
    }
    if (ruleForm.phone.length === 0) {
      ElNotification({
          title: "账号不能为空",
          type: "success",
        });
     

      
           
       }
    if (ruleForm.pass.length === 0) {
      ElNotification({
          title: "未注册",
          type: "success",
          position:"bottom-left"
        });

     
           
        }

    

  });
};
</script>

<style scoped>
.login {
  height: 100%;
  background-image: url(../assets/1.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-form {
  background-color: white;
  padding: 30px;
  min-width: 400px;
  border-radius: 10px;
}
.title {
  margin-bottom: 30px;
  font-size: 20px;
  font-weight: bold;
  text-align: center;
}
</style>